package com.woan.api.services;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysPhotoIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.util.ConfigUtil;
import com.woan.api.vo.VphotoList;



public class PhotoServerServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private SysPhotoIDAO sysphotoiDAO;
	
	public SysPhotoIDAO getSysphotoiDAO() {
		return sysphotoiDAO;
	}



	public void setSysphotoiDAO(SysPhotoIDAO sysphotoiDAO) {
		this.sysphotoiDAO = sysphotoiDAO;
	}



	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}



	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}


	
	public List<VphotoList> listPhotos(String rid,Integer beginIndex,Integer endIndex){
		List<VphotoList> photo = null;
		photo = sysphotoiDAO.FindVPhotoByGroup(rid, beginIndex, endIndex);
		tranIp(photo);
		return photo;
	}
	public int listPhotosCount(String groupname,String username,String userpwd){
		
		SysAdmininfo user = null;
		user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname,username,userpwd);
		
		return  sysphotoiDAO.FindPhotosCountByGroup(user.getSysRole().getRid());
	}
	public List<VphotoList> FindVSerachPhotoByCon(String groupname,String username,String userpwd,Integer beginIndex,Integer endIndex,Map<String, String> map)
	{
		SysAdmininfo user = null;
		user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(groupname,username,userpwd);
		List<VphotoList> photo = null;
		photo = sysphotoiDAO.FindVSerachPhotoByCon(user.getSysRole().getRid(), beginIndex, endIndex, map);
		tranIp(photo);
		return photo;
	}
	public JSONObject editPhotos(String groupname,String username,String pwd,String photoid,String title){
		String flag="0";
		JSONObject json=new JSONObject();
		flag = sysphotoiDAO.EditPhotos(photoid, title);
		if(flag.equals("0"))
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "修改成功");
		}
		else
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "修改失败");
		}
		return json;
	}
	public JSONObject deletePhotos(String groupname,String username,String pwd,String devicenum,String photoid){
		boolean flag = true;
		if(!photoid.equals("") && photoid!=null)
		{
			String []arrayphoto = photoid.split(",");
			for (int i = 0 ;i<arrayphoto.length;i++)
			{
				if(arrayphoto[i]!=null && !arrayphoto[i].equals(""))
					flag=sysphotoiDAO.DeletePhotos(arrayphoto[i]);
			}
		}
		JSONObject json=new JSONObject();
		if(flag)
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "删除成功");
		}
		else
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "删除失败");
		}
		return json;
	}
	/**
	 * 格式化内网ip
	 */
	private void tranIp(List<VphotoList> photos) {
		String neiwang_ip = ConfigUtil.get("neiwang_ip");
		if (photos.size()>0) {
			for (VphotoList photo : photos) {
				String url = photo.getUrl();
				if (url!=null&&url.length()>1) {
					String substring = url.substring(url.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					photo.setUrl(newPathString);
				}
				String smurl =photo.getSmurl();
				if (smurl!=null&&smurl.length()>1) {
					String substring = smurl.substring(smurl.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					photo.setSmurl(newPathString);
				}
			}
		}
	}
}
